<?php

class User_relateController extends BaseController {
	//我是否关注ta
	public function isFollowAction() {
		$toUid = intval(Request::getParams('toUid'));
		if ($relateInfo = self::model('user_relate')->getOne('uid=? and to_uid=?', [$this->user_info['uid'], $toUid])) {
			Response::ajax(200, 'success', [
				'isFollow' => 1,
			]);
		}
		Response::ajax(200, 'success', [
			'isFollow' => 0,
		]);
	}

	//ta是否关注我
	public function isFansAction() {
		$toUid = intval(Request::getParams('toUid'));
		if ($relateInfo = self::model('user_relate')->getOne('uid=? and to_uid=?', [$toUid, $this->user_info['uid']])) {
			Response::ajax(200, 'success', [
				'isFans' => 1,
			]);
		}
		Response::ajax(200, 'success', [
			'isFans' => 0,
		]);
	}

	public function getFansByUidAction() {
		$uid = intval(Request::getParams('uid'));
		$myUid = $this->user_info['uid'];
		$lastTimeStamp = Request::getParams('lastTimeStamp');
		$pageSize = Request::getParams('pageSize', 10);
		if (!$lastTimeStamp) {
			$lastTimeStamp = time();
		}
		Response::ajax(200, 'success', self::model('user_relate')->
				getFansListByUid($uid, $myUid, $lastTimeStamp, $pageSize));
	}

	public function getFollowByUidAction() {
		$uid = intval(Request::getParams('uid'));
		$myUid = $this->user_info['uid'];
		$lastTimeStamp = Request::getParams('lastTimeStamp');
		$pageSize = Request::getParams('pageSize', 10);
		if (!$lastTimeStamp) {
			$lastTimeStamp = time();
		}
		Response::ajax(200, 'success', self::model('user_relate')->
				getFollowListByUid($uid, $myUid, $lastTimeStamp, $pageSize));
	}

	public function getFollowByUidDakaAction() {
		$myUid = $this->user_info['uid'];
		$page = Request::getParams('page', 1);
		$pageSize = Request::getParams('pageSize', 10);
		$limit = ($page - 1) * $page_size;
		// $sql = "SELECT r.to_uid FROM wf_user_relate r,wf_user u WHERE r.to_uid=u.uid and r.uid =210 AND type > 2 limit {$limit}, {$pageSize}";
		// $result = self::model('user_relate')->query($sql)->fetchAll();
		// var_dump($result);
		// exit;
		Response::ajax(200, 'success', self::model('user_relate')->
				getFollowListByUids($myUid, $page, $pageSize));
	}

	//我的粉丝
	public function fansListAction() {
		$lastTimeStamp = Request::getParams('lastTimeStamp');
		$pageSize = Request::getParams('pageSize', 10);
		if (!$lastTimeStamp) {
			$lastTimeStamp = time();
		}
		Response::ajax(200, 'success', self::model('user_relate')->
				getFansList($this->user_info['uid'], $lastTimeStamp, $pageSize));
	}

	//我关注的人
	public function followListAction() {
		$lastTimeStamp = Request::getParams('lastTimeStamp');
		$pageSize = Request::getParams('pageSize', 10);
		if (!$lastTimeStamp) {
			$lastTimeStamp = time();
		}
		Response::ajax(200, 'success', self::model('user_relate')->
				getFollowList($this->user_info['uid'], $lastTimeStamp, $pageSize));
	}

	public function addAction() {
		$uid = $this->user_info['uid'];
		if (!$userInfo = self::model('user')->findById($uid)) {
			Response::ajax(USER_NO_LOGIN, '用户未登录');
		}
		$toUid = intval(Request::getParams('toUid'));
		if ($uid == $toUid) {
			Response::ajax(NORMAL_ERROR, '参数错误,您无法关注自己');
		}
		if (!$toUid || !$toUserInfo = self::model('user')->getOne('uid=?', $toUid)) {
			Response::ajax(NORMAL_ERROR, '用户参数传递有误');
		}
		try {
			if (self::model('user_relate')->insert([
				'uid' => $uid,
				'to_uid' => $toUid,
				'add_time' => time(),
			])) {
				self::model('user')->execute('update wf_user set follow_total=follow_total+1 where uid=?', [$uid]);
				self::model('user')->execute('update wf_user set fans_total=fans_total+1 where uid=?', [$toUid]);

				user_notifyModel::addNotify($toUid, notifyModel::TYPE_FOLLOW, $uid, $userInfo);

				Response::ajax(200, '关注成功');
			}

		} catch (Exception $exc) {
			Log::write('创建关注失败:' . $exc->getTraceAsString(), 'WARN');
		}
		Response::ajax(200, '您已关注');
	}

	public function removeAction() {
		$uid = $this->user_info['uid'];
		$toUid = intval(Request::getParams('toUid'));
		if (!$toUid || !$toUserInfo = self::model('user')->getOne('uid=?', $toUid)) {
			Response::ajax(NORMAL_ERROR, '用户参数传递有误');
		}
		try {
			if (self::model('user_relate')->delete('uid=? and to_uid=?', [$uid, $toUid])) {
				self::model('user')->execute('update wf_user set follow_total=follow_total-1 where uid=?', [$uid]);
				self::model('user')->execute('update wf_user set fans_total=fans_total-1 where uid=?', [$toUid]);
				Response::ajax(200, '取消关注成功');
			}
		} catch (Exception $exc) {
			Log::write('取消关注失败:' . $exc->getTraceAsString(), 'WARN');
		}
		Response::ajax(200, '您已取消关注');
	}
}